// Problem: 
// Contest: 
// URL: 
// Memory Limit: 
// Time Limit: 
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
	return s*w;
}
signed main()
{
	int T=1;
//	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		vector<int> l(n+1),r(n+1);
		vector<vector<int>> v(n+1,vector<int>(2));
		for(int i=1;i<=n;i++)
		{
			cin>>l[i]>>r[i];
		}
		v[1][0]=r[1]-1+r[1]-l[1];
		v[1][1]=r[1]-1;
		for(int i=2;i<=n;i++)
		{
			v[i][0]=min(v[i-1][0]+abs(l[i-1]-r[i]),v[i-1][1]+abs(r[i-1]-r[i]))+r[i]-l[i]+1;
			v[i][1]=min(v[i-1][1]+abs(r[i-1]-l[i]),v[i-1][0]+abs(l[i-1]-l[i]))+r[i]-l[i]+1;
		}
		cout<<min(v[n][0]+n-l[n],v[n][1]+n-r[n]);
	}
	return 0;
}
